package cn.com.coding.system.service.impl;

import cn.com.coding.common.utils.PageUtil;
import cn.com.coding.common.utils.Query;
import cn.com.coding.system.dao.ProductCategoryDao;
import cn.com.coding.system.dao.ProductImageDao;
import cn.com.coding.system.dao.ProductInfoDao;
import cn.com.coding.system.dao.UserInfoDao;
import cn.com.coding.system.domin.ProductImageEntity;
import cn.com.coding.system.domin.ProductInfoEntity;
import cn.com.coding.system.domin.dto.ProductInfoDto;
import cn.com.coding.system.service.ProductInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("productInfoService")
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoDao, ProductInfoEntity> implements ProductInfoService {

    @Resource
    private ProductImageDao productImageDao;

    @Resource
    private ProductCategoryDao productCategoryDao;

    @Resource
    private UserInfoDao userInfoDao;

    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        params.put("limit", params.get("limit").toString());
        IPage<ProductInfoDto> page = null;
        if (params.get("page") != null) {
            params.put("page", params.get("page").toString());

            page = this.baseMapper.selectMyPage(new Query<ProductInfoDto>().getPage(params), new QueryWrapper<ProductInfoDto>()
                    .and(wrapper -> wrapper
                            .eq("pc.category_name", params.get("name") != null ? params.get("name") : "")
                            .eq("pi.category_pid", params.get("cateGoryPid") != null ? params.get("cateGoryPid") : "")
                            .or()
                            .like("pi.product_name", params.get("name") != null ? params.get("name") : "")
                    ));
        } else {
            page = this.baseMapper.selectMyPage(new Query<ProductInfoDto>().getPage(params), new QueryWrapper<ProductInfoDto>()
                    .and(wrapper -> wrapper
                            .eq("pi.category_pid", params.get("cateGoryPid") != null ? params.get("cateGoryPid") : "")
                    ));
        }
        page.getRecords().forEach(e -> {
            List<String> list = productImageDao.selectList(new QueryWrapper<ProductImageEntity>()
                    .eq("product_pid", e.getPid())).stream().map(ProductImageEntity::getImgUrl).collect(Collectors.toList());
            e.setImgUrl(list);
        });

        return new PageUtil(page);
    }

    @Override
    public PageUtil queryPageByCategory(Map<String, Object> params) {

        IPage<ProductInfoDto> page = this.baseMapper
                .selectMyPage(new Query<ProductInfoDto>()
                        .getPage(params), new QueryWrapper<ProductInfoDto>()
                        .eq("product_type", 1)
                        .and(e -> e.eq("pi.category_pid", params
                                        .get("categoryID"))
                                .or()
                                .eq("gc.catelog_name", params
                                        .get("categoryName")))

                );

        page.getRecords().forEach(e -> {
            List<String> list = productImageDao.selectList(new QueryWrapper<ProductImageEntity>().eq("product_pid", e.getPid())).stream().map(ProductImageEntity::getImgUrl).collect(Collectors.toList());
            e.setImgUrl(list);
        });

        return new PageUtil(page);
    }

    @Override
    public PageUtil getHotProduct(Map<String, Object> params) {
        QueryWrapper<ProductInfoDto> eq = new QueryWrapper<ProductInfoDto>()
                .eq("product_type", 1)
                .orderByDesc("update_time");
        if (params.get("activityStatus") != null && Integer.parseInt(params.get("activityStatus").toString())!= 0) {
            eq.eq("activity_status", params.get("activityStatus"));
        }
        if (params.get("productName") != null && params.get("productName") != "") {
            eq.like("product_name", params.get("productName"));
        }
        if (params.get("page") != null) {
            params.put("page", params.get("page").toString());
        }
        if (params.get("limit") != null) {
            params.put("limit", params.get("limit").toString());
        }
        IPage<ProductInfoDto> page = this.baseMapper
                .selectMyPage(new Query<ProductInfoDto>()
                        .getPage(params), eq);
        page.getRecords().forEach(e -> {
            List<String> list = productImageDao.selectList(new QueryWrapper<ProductImageEntity>()
                            .eq("product_pid", e.getPid()))
                    .stream()
                    .map(ProductImageEntity::getImgUrl)
                    .collect(Collectors.toList());
            e.setImgUrl(list);
        });
        return new PageUtil(page);
    }

    @Override
    public PageUtil getAllBuy(Map<String, Object> params) {
        IPage<ProductInfoDto> page = this.baseMapper.selectMyPage(new Query<ProductInfoDto>().getPage(params), new QueryWrapper<ProductInfoDto>().eq("product_type", 0).orderByDesc("update_time"));
        return new PageUtil(page);
    }

}